﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Temperature
{
    public class Temperature:ITemperature
    {
        public void InsertTemperature(int cityId, ref Model.NutriLeafDBEntities context)
        {
          // nutrisensep art
      
            throw new NotImplementedException();
        }

        public void InsertTemperature(int cityId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            //nutrisense part
            throw new NotImplementedException();
        }

        public void InsertTemperature(int cityId, double averageTemp, double maximumTemperature, double minimumTemperature, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary temp = new Model.TemperatureDiary();
            temp.CityID = cityId;
            temp.AverageTemperature = new Decimal(averageTemp);
            temp.MaximumTemperature = new decimal(maximumTemperature);
            temp.MinimumTemperature = new decimal(minimumTemperature);
            context.TemperatureDiaries.AddObject(temp);
            context.SaveChanges();
        }

        public void InsertTemperature(int cityId, double averageTemp, double maximumTemperature, double minimumTemperature, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary temp = new Model.TemperatureDiary();
            temp.CityID = cityId;
            temp.AverageTemperature = new Decimal(averageTemp);
            temp.MaximumTemperature = new decimal(maximumTemperature);
            temp.MinimumTemperature = new decimal(minimumTemperature);
            temp.DateTimeStamp = datetime;
            context.TemperatureDiaries.AddObject(temp);
           

            context.SaveChanges();
        }

        public void InsertTemperature(string city, ref Model.NutriLeafDBEntities context)
        {
            //nutrisense part
            throw new NotImplementedException();
        }

        public void InsertTemperature(string city, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            //nutrisense part
            throw new NotImplementedException();
        }

        public void InsertTemperature(string city, double averageTemp, double maximumTemperature, double minimumTemperature, ref Model.NutriLeafDBEntities context)
        {
            //nutrisense part
            throw new NotImplementedException();
        }

        public void InsertTemperature(string city, double averageTemp, double maximumTemperature, double minimumTemperature, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary temp = new Model.TemperatureDiary();
            temp.City.CityName = city;
            temp.MaximumTemperature = new decimal(maximumTemperature);
            temp.MinimumTemperature = new decimal(minimumTemperature);
            temp.DateTimeStamp = datetime;
            context.TemperatureDiaries.AddObject(temp);
            context.SaveChanges();

        }

        public void InsertTemperature(Model.TemperatureDiary temperature, ref Model.NutriLeafDBEntities context)
        {
            context.TemperatureDiaries.AddObject(temperature);
            context.SaveChanges();
        }

        public double GetAverageTemperature(string city, ref Model.NutriLeafDBEntities context)
        {
           
        
            return (double) context.Cities.Where(p => p.CityName.CompareTo(city) == 0).First().TemperatureDiaries.First().AverageTemperature;
        }

        public double GetAverageTemperature(int cityId, ref Model.NutriLeafDBEntities context)
        {
        
             
        
            return (double) context.Cities.Where(p => p.CityID==cityId).First().TemperatureDiaries.First().AverageTemperature;
        
        }

        public double GetAverageTemperature(string city, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
          
        
           Model.TemperatureDiary[] dairy=context.Cities.Where(p=>p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
        return (double)dairy.Where(p=>p.DateTimeStamp.CompareTo(datetime)==0).First().AverageTemperature;
        }
        

        public double GetAverageTemperature(int cityId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID==cityId).First().TemperatureDiaries.ToArray();
            return (double)dairy.Where(p => p.DateTimeStamp.CompareTo(dateTime) == 0).First().AverageTemperature;
        }

        public double GetMinTemperature(string city, ref Model.NutriLeafDBEntities context)
        {
            
            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
            return (double)dairy.First().MinimumTemperature;
        
        }

        public double GetMinTemperature(int cityId, ref Model.NutriLeafDBEntities context)
        {

            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID==cityId).First().TemperatureDiaries.ToArray();
            return (double)dairy.First().MinimumTemperature;
        

        }

        public double GetMinTemperature(string city, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
            return (double)dairy.First().MinimumTemperature;
        
           
        }

        public double GetMinTemperature(int cityId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {

            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID == cityId).First().TemperatureDiaries.ToArray();
            return (double)dairy.Where(p=>p.DateTimeStamp.CompareTo(dateTime)==0).First().MinimumTemperature;
        
        }

        public double GetMaxTemperature(string city, ref Model.NutriLeafDBEntities context)
        {
            
            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
            return (double)dairy.First().MaximumTemperature;
        }
        

        public double GetMaxTemperature(int cityId, ref Model.NutriLeafDBEntities context)
        {

            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID == cityId).First().TemperatureDiaries.ToArray();
            return (double)dairy.First().MaximumTemperature;
        
        }

        public double GetMaxTemperature(string city, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {

            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
            return (double)dairy.Where(p=>p.DateTimeStamp.CompareTo(datetime)==0).First().MinimumTemperature;
        
        }

        public double GetMaxTemperature(int cityId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {

            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID == cityId).First().TemperatureDiaries.ToArray();
            return (double)dairy.Where(p=>p.DateTimeStamp.CompareTo(dateTime)==0).First().MaximumTemperature;
        
        }

        public Model.TemperatureDiary GetTemperature(string city, ref Model.NutriLeafDBEntities context)
        {

           return context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.First();
            
        
        }

        public Model.TemperatureDiary GetTemperature(int cityId, ref Model.NutriLeafDBEntities context)
        {

            return context.Cities.Where(p => p.CityID == cityId).First().TemperatureDiaries.ToArray().First();
            
        
        }

        public Model.TemperatureDiary GetTemperature(string city, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
        Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityName.CompareTo(city)==0).First().TemperatureDiaries.ToArray();
            return dairy.Where(p=>p.DateTimeStamp.CompareTo(datetime)==0).First();
        
        }
        

        public Model.TemperatureDiary GetTemperature(int cityId, DateTime dateTime, ref Model.NutriLeafDBEntities context)
        {
            Model.TemperatureDiary[] dairy = context.Cities.Where(p => p.CityID == cityId).First().TemperatureDiaries.ToArray();
            return dairy.Where(p=>p.DateTimeStamp.CompareTo(dateTime)==0).First();
        
        }
        

        public void UpdateTemperature(int temperatureId, int cityId, double averageTemp, double maximumTemperature, double minimumTemperature, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void RemoveTemperature(int temperatureId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.TemperatureDiaries.Where(p => p.TemperatureID == temperatureId).First());
            context.SaveChanges();
        }
    }
}